package nl.sportan.samenloop.adapter;

import nl.sportan.samenloop.R;
import nl.sportan.samenloop.communication.PhotoTarget;
import nl.sportan.samenloop.communication.flickr.PhotoDataRetriever;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Handler;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gmail.yuyang226.flickr.photos.Photo;
import com.gmail.yuyang226.flickr.photos.PhotoList;
import com.gmail.yuyang226.flickr.photos.Size;

public class ImageListPageAdapter extends PagerAdapter {

	private PhotoList photoList;

	private final Context context;
	private Handler handler;

	public ImageListPageAdapter(Context context, PhotoList photoList) {
		this.context = context;
		this.photoList = photoList;
		handler = new Handler();
	}

	private View createImageView(View pager, int position) {
		View v = (LinearLayout) LayoutInflater.from(context).inflate(
				R.layout.image_view, ((ViewPager) pager), false);

		ImageView iv = (ImageView) v.findViewById(R.id.image);
		
		Photo p = photoList.get(position);
		iv.setOnLongClickListener(new PhotoClickListener(p));
		
		
		String title = p.getTitle();

		TextView titleView = (TextView) v.findViewById(R.id.title);
		titleView.setText(title);

		Thread t = new Thread(new PhotoDataRetriever(context, p, Size.LARGE,
				new PhotoHolder(iv), false));
		t.start();

		
		return v;
	}

	@Override
	public int getCount() {
		return photoList.size();
	}

	@Override
	public Object instantiateItem(View pager, int position) {
		View v = createImageView(pager, position);
		((ViewPager) pager).addView(v, 0);

		return v;
	}

	@Override
	public void destroyItem(View pager, int position, Object view) {
		((ViewPager) pager).removeView((LinearLayout) view);
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view.equals(object);
	}

	@Override
	public void finishUpdate(View view) {
	}

	@Override
	public void restoreState(Parcelable p, ClassLoader c) {
	}

	@Override
	public Parcelable saveState() {
		return null;
	}

	@Override
	public void startUpdate(View view) {
	}

	private class PhotoHolder implements PhotoTarget {

		private ImageView target;

		public PhotoHolder(ImageView target) {
			this.target = target;
		}

		public void setPhoto(Bitmap bitmap) {
			handler.post(new PhotoUpdater(bitmap, target));
		}

	}

	private class PhotoUpdater implements Runnable {

		private Bitmap bitmap;
		private ImageView target;

		public PhotoUpdater(Bitmap bitmap, ImageView target) {
			this.bitmap = bitmap;
			this.target = target;
		}

		public void run() {
			target.setImageBitmap(bitmap);
		}

	}


	private class PhotoClickListener implements OnLongClickListener {

		private Photo photo;
		
		public PhotoClickListener(Photo photo) {
			this.photo = photo;
		}
		
		public boolean onLongClick(View v) {
			Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
			
			shareIntent.setType("image/jpeg");
			
			Uri photoUri = Uri.parse(photo.getMediumUrl());
			
			shareIntent.setData(photoUri);
			/*shareIntent.putExtra(android.content.Intent.EXTRA_TITLE,
					photo.getTitle());
			
			shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
					photo.getTitle());*/
			shareIntent.putExtra(android.content.Intent.EXTRA_STREAM,
					photoUri);

			context.startActivity(Intent.createChooser(shareIntent, "Deel Foto"));
			
			return true;
		}
		
	}
	
}